package repeater

import (
    "math"
    "sort"
    "encoding/json"
)

type Report struct {
    Ip    string             `json:"ip"`
    Day   int                `json:"day"`
    Metic ReportMetric       `json:"metric"`
    Data  map[string]float64 `json:"data"`
}

func (r Report) toByte() []byte {
    b, err := json.Marshal(r)
    if err != nil {
        return nil
    }
    return b
}

func ngStatic(in Value) map[string]float64 {
    ret := make(map[string]float64)
    var sum float64
    for _, v := range in {
        sum += v
    }
    d := sort.Float64Slice(in)
    d.Sort()
    size := len(d)
    ret["count"] = float64(size)
    avg := sum / ret["count"]
    ret["avg"] = avg
    idx := int(float64(size) * 0.5)
    ret["50th"] = d[idx]
    idx = int(float64(size) * 0.8)
    ret["80th"] = d[idx]
    idx = int(float64(size) * 0.9)
    ret["90th"] = d[idx]
    idx = int(float64(size) * 0.95)
    ret["95th"] = d[idx]
    if size == 0 {
        return ret
    }
    ret["max"] = d[size-1]
    ret["min"] = d[0]
    var v float64
    for _, f := range in {
        v += (f - avg) * (f - avg)
    }
    ret["variance"] = v / float64(size)
    for k, v := range ret {
        if v == math.NaN() {
            log.Error("origin is %v , %v is NaN %v", in, k, v)
            break
        }
    }
    return ret
}
